'''
Created on 28/07/2010

@author: eh01
'''
import unittest
from datastore.spaces import Spaces
from datastore.uri import Uri
from adaptor.services.accept_reject_service import AcceptRejectService
from datastore.resource import Resource

class AcceptRejectServiceTest(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass

    def test_cb_start(self):
        spaces = Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        accept_reject_service.cb_start(None, None, None)
        self.assertEqual(accept_reject_service.cb_accept_reject_application, spaces.process[Uri('internal:adaptor/accept-reject/')][0])
        self.assertEqual(accept_reject_service.cb_reject_application, spaces.process[Uri('internal:adaptor/accept-reject/reject/')][0])
                
    def test_cb_accept_reject_application_resposne(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        ret = accept_reject_service.cb_accept_reject_application(Uri("something/otherthing/response"), None, None)
        self.assertEqual(None, ret)
    
    def test_cb_accept_reject_application_get(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        res = accept_reject_service.cb_accept_reject_application(Uri("dummy/uri"), Spaces.ACTION_GET, None)
        self.assertEqual(None, res)

    def test_cb_accept_reject_application_delete(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        ret = accept_reject_service.cb_accept_reject_application(Uri("dummy/uri"), Spaces.ACTION_DELETE, None)
        self.assertEqual(None, ret)
        
    def test_cb_accept_reject_application_remove(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        res = accept_reject_service.cb_accept_reject_application(Uri("dummy/uri"), Spaces.ACTION_REMOVE, None)
        self.assertEqual(None, res)
        
    def test_cb_accept_reject_application_update(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        uri = accept_reject_service.cb_accept_reject_application(Uri("dummy/uri"), Spaces.ACTION_UPDATE, None)
        self.assertEqual(None, uri)
        
    def test_cb_accept_reject_application_find(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        lst = accept_reject_service.cb_accept_reject_application(Uri("dummy/uri"), Spaces.ACTION_FIND, None)
        self.assertEqual(None, lst)
    
    def test_cb_accept_reject_application_put_form_fields_OK_ACCEPT(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.OK,
                           AcceptRejectService.SPEAR_REF:spear_reference,
                         AcceptRejectService.OUTCOME:AcceptRejectService.OUTCOME_ACCEPT}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_accept_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:base/services/http/response'), response_rs.get_type())
        self.assertEqual(None, response_rs.content)
        self.assertEqual(None, response_rs.content_type)
        self.assertEqual(0, response_rs.content_length)
        self.assertEqual(302, response_rs.status)
        self.assertEqual({'Location':'/adaptor/application?spearRef=%s' % spear_reference}, response_rs.headers)
        
    def test_cb_accept_reject_application_put_form_fields_OK_ACCEPT_WITH_CHANGES(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.OK,
                           AcceptRejectService.SPEAR_REF:spear_reference,
                         AcceptRejectService.OUTCOME:AcceptRejectService.OUTCOME_ACCEPT_WITH_CHANGES}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_accept_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:base/services/http/response'), response_rs.get_type())
        self.assertEqual(None, response_rs.content)
        self.assertEqual(None, response_rs.content_type)
        self.assertEqual(0, response_rs.content_length)
        self.assertEqual(302, response_rs.status)
        self.assertEqual({'Location':'/adaptor/application?spearRef=%s' % spear_reference}, response_rs.headers)
        
    def test_cb_accept_reject_application_put_form_fields_OK_REJECT(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.OK,
                           AcceptRejectService.SPEAR_REF:spear_reference,
                         AcceptRejectService.OUTCOME:AcceptRejectService.OUTCOME_REJECT}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_accept_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:base/services/http/response'), response_rs.get_type())
        self.assertEqual(None, response_rs.content)
        self.assertEqual(None, response_rs.content_type)
        self.assertEqual(0, response_rs.content_length)
        self.assertEqual(302, response_rs.status)
        self.assertEqual({'Location':'/adaptor/accept-reject/reject?spearRef=%s' % spear_reference}, response_rs.headers)
                
    def test_cb_accept_reject_application_put_form_fields_fail_validation(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        accept_reject_service.get_spear_user_list = self.mock_get_spear_user_list
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.OK,
                           AcceptRejectService.SPEAR_REF:spear_reference}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_accept_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri("term:eplanning/adaptor/acceptreject/get/data"), response_rs.get_type())
        self.assertEqual("Accept or Reject Application", response_rs.h1_header)
        self.assertEqual(self.mock_get_spear_user_list(None), response_rs.user_list)
        self.assertTrue(response_rs.validation_error)
        self.assertEqual(spear_reference, response_rs.spear_ref)
        
    def test_cb_accept_reject_application_put_form_fields_cancel(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.CANCEL,
                           AcceptRejectService.SPEAR_REF:spear_reference}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_accept_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:base/services/http/response'), response_rs.get_type())
        self.assertEqual(None, response_rs.content)
        self.assertEqual(None, response_rs.content_type)
        self.assertEqual(0, response_rs.content_length)
        self.assertEqual(302, response_rs.status)
        self.assertEqual({'Location':'/adaptor/application?spearRef=%s' % spear_reference}, response_rs.headers)     
        
    def test_cb_accept_reject_application_put_query_data(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        accept_reject_service.get_spear_user_list = self.mock_get_spear_user_list
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {}
        res.query_data = {AcceptRejectService.SPEAR_REF:spear_reference}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_accept_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri("term:eplanning/adaptor/acceptreject/get/data"), response_rs.get_type())
        self.assertEqual("Accept or Reject Application", response_rs.h1_header)
        self.assertEqual(self.mock_get_spear_user_list(None), response_rs.user_list)
        self.assertEqual(spear_reference, response_rs.spear_ref) 
        
    def test_cb_accept_reject_application_exists(self):
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        uri = Uri("dummy/uri")
        ret = accept_reject_service.cb_accept_reject_application(uri, Spaces.ACTION_EXISTS, None)
        self.assertFalse(ret)              

    def test_cb_reject_application_resposne(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        ret = accept_reject_service.cb_reject_application(Uri("something/otherthing/response"), None, None)
        self.assertEqual(None, ret)
    
    def test_cb_reject_application_get(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        res = accept_reject_service.cb_reject_application(Uri("dummy/uri"), Spaces.ACTION_GET, None)
        self.assertEqual(None, res)

    def test_cb_reject_application_delete(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        ret = accept_reject_service.cb_reject_application(Uri("dummy/uri"), Spaces.ACTION_DELETE, None)
        self.assertEqual(None, ret)
        
    def test_cb_reject_application_remove(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        res = accept_reject_service.cb_reject_application(Uri("dummy/uri"), Spaces.ACTION_REMOVE, None)
        self.assertEqual(None, res)
        
    def test_cb_reject_application_update(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        uri = accept_reject_service.cb_reject_application(Uri("dummy/uri"), Spaces.ACTION_UPDATE, None)
        self.assertEqual(None, uri)
        
    def test_cb_reject_application_find(self):
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        lst = accept_reject_service.cb_reject_application(Uri("dummy/uri"), Spaces.ACTION_FIND, None)
        self.assertEqual(None, lst)

    def test_cb_reject_application_put_form_fields_OK(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        accept_reject_service.do_accept_reject_application = self.mock_do_accept_reject_application
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.OK,
                           AcceptRejectService.SPEAR_REF:spear_reference,
                         AcceptRejectService.REASON:'Some Reason'}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:base/services/http/response'), response_rs.get_type())
        self.assertEqual(None, response_rs.content)
        self.assertEqual(None, response_rs.content_type)
        self.assertEqual(0, response_rs.content_length)
        self.assertEqual(302, response_rs.status)
        self.assertEqual({'Location':'/adaptor/application?spearRef=%s' % spear_reference}, response_rs.headers)  
        
    def test_cb_reject_application_put_form_fields_fail_validation(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.OK,
                           AcceptRejectService.SPEAR_REF:spear_reference}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri("term:eplanning/adaptor/acceptreject/reject/get/data"), response_rs.get_type())
        self.assertEqual("Reject Application Reason", response_rs.h1_header)
        self.assertEqual(spear_reference, response_rs.spear_ref)
        self.assertTrue(response_rs.validation_error)                

    def test_cb_reject_application_put_form_fields_cancel(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {AcceptRejectService.OPERATION:AcceptRejectService.CANCEL,
                           AcceptRejectService.SPEAR_REF:spear_reference}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri('term:base/services/http/response'), response_rs.get_type())
        self.assertEqual(None, response_rs.content)
        self.assertEqual(None, response_rs.content_type)
        self.assertEqual(0, response_rs.content_length)
        self.assertEqual(302, response_rs.status)
        self.assertEqual({'Location':'/adaptor/application?spearRef=%s' % spear_reference}, response_rs.headers)   

    def test_cb_reject_application_put_query_data(self):
        spear_reference = "S000001E"
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        accept_reject_service.get_spear_user_list = self.mock_get_spear_user_list
        res = Resource(type="term:base/services/http/post")
        res.form_fields = {}
        res.query_data = {AcceptRejectService.SPEAR_REF:spear_reference}
        uri = Uri("dummy/uri")
        response_uri = accept_reject_service.cb_reject_application(uri, Spaces.ACTION_PUT, res)
        self.assertEqual(uri + AcceptRejectService.RESPONSE, response_uri)
        response_rs = spaces.get(response_uri)
        self.assertEqual(Uri("term:eplanning/adaptor/acceptreject/reject/get/data"), response_rs.get_type())
        self.assertEqual("Reject Application Reason", response_rs.h1_header)
        self.assertEqual(spear_reference, response_rs.spear_ref) 
                    
    def test_cb_reject_application_exists(self):
        spaces=Spaces()
        accept_reject_service = AcceptRejectService(spaces, uri=Uri())
        uri = Uri("dummy/uri")
        ret = accept_reject_service.cb_reject_application(uri, Spaces.ACTION_EXISTS, None)
        self.assertFalse(ret) 
    
    def test_create_response_resource(self):
        content = "HAHAHAHA"
        content_type = "text/plain"
        status = 200
        headers = {"key_1":"value_2","key_2":"value_2"}
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        res = accept_reject_service.create_response_resource(content, content_type, status, headers)
        self.assertEqual(content, res.content)
        self.assertEqual(content_type, res.content_type)
        self.assertEqual(len(content), res.content_length)
        self.assertEqual(status, res.status)
        self.assertEqual(headers, res.headers)
        
        content = ""
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        res = accept_reject_service.create_response_resource(content, content_type, status, headers)
        self.assertEqual(content, res.content)
        self.assertEqual(content_type, res.content_type)
        self.assertEqual(len(content), res.content_length)
        self.assertEqual(status, res.status)
        self.assertEqual(headers, res.headers)  
        
        content = None
        content_type = None
        accept_reject_service = AcceptRejectService(spaces=Spaces(), uri=Uri())
        res = accept_reject_service.create_response_resource(content, content_type, status, headers)
        self.assertEqual(content, res.content)
        self.assertEqual(content_type, res.content_type)
        self.assertEqual(0, res.content_length)
        self.assertEqual(status, res.status)
        self.assertEqual(headers, res.headers)                

    def mock_get_spear_user_list(self, spaces):
        return [{'first_name':'John','last_name':'Doe'}]
    
    def mock_do_accept_reject_application(self, spaces, spear_reference, reason):
        return   
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()